<template>
  <div class="login-container">
    <div class="login-box">
      <h2>Login</h2>
      <form @submit.prevent="handleLogin">
        <div class="input-group" :class="{ focus: isFocused.username }">
          <input
            type="text"
            v-model="username"
            @focus="isFocused.username = true"
            @blur="isFocused.username = false"
            placeholder="Username"
          />
        </div>
        <div class="input-group" :class="{ focus: isFocused.password }">
          <input
            type="password"
            v-model="password"
            @focus="isFocused.password = true"
            @blur="isFocused.password = false"
            placeholder="Password"
          />
        </div>
        <button type="submit" :disabled="isLoading">
          <span v-if="!isLoading">Login</span>
          <span v-else class="loading-spinner"></span>
        </button>
      </form>
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
import request from "../util/request";
export default {
  setup() {
    const username = ref("superadmin");
    const password = ref("91888");
    const isLoading = ref(false);
    const isFocused = ref({ username: false, password: false });

    const handleLogin = async () => {
      if (!username.value || !password.value) {
        alert("Please fill in all fields.");
        return;
      }
      isLoading.value = true;
      try {
        // 模拟登录请求
        const data=await request.post("/admin/login/", {
          phone: username.value,
          password: password.value,
        })
        console.log(data);
        // https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png
        localStorage.setItem("token", data.data.data.token);
        localStorage.setItem("role", data.data.data.user.role);
        localStorage.setItem("avatar", data.data.data.user.avatar?data.data.data.user.avatar
        :"https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png");
        localStorage.setItem("nickname", data.data.data.user.nickname);
        // router.push("/");
        window.location.href = "/";
      } catch (error) {
        console.log(error);
        
        alert("Login failed. Please try again.");
      } finally {
        isLoading.value = false;
      }
    };

    return {
      username,
      password,
      isLoading,
      isFocused,
      handleLogin,
    };
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: linear-gradient(120deg, #84fab0, #8fd3f4);
  font-family: Arial, sans-serif;
}

.login-box {
  background: white;
  border-radius: 10px;
  padding: 20px 30px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  text-align: center;
  width: 350px;
}

h2 {
  margin-bottom: 20px;
  color: #333;
}

.input-group {
  position: relative;
  margin-bottom: 20px;
}

.input-group input {
  width: 100%;
  padding: 10px;
  border: 2px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  outline: none;
  transition: 0.3s;
}

.input-group.focus input {
  border-color: #84fab0;
}

button {
  background: linear-gradient(120deg, #84fab0, #8fd3f4);
  color: white;
  font-size: 16px;
  border: none;
  padding: 10px 20px;
  border-radius: 5px;
  cursor: pointer;
  transition: 0.3s;
  position: relative;
}

button:hover {
  background: linear-gradient(120deg, #8fd3f4, #84fab0);
}

button:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.loading-spinner {
  border: 4px solid #fff;
  border-top: 4px solid #84fab0;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  animation: spin 1s linear infinite;
  display: inline-block;
}

@keyframes spin {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>
